package com.bdzl.admin.controller.sys;

import java.util.List;
import java.util.Map;

import com.bdzl.pub.controller.BaseController;
import org.springframework.stereotype.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bdzl.pub.entity.sys.DictDO;
import com.bdzl.admin.service.sys.DictService;
import com.bdzl.pub.util.param.PageUtils;
import com.bdzl.pub.util.param.Query;
import com.bdzl.pub.util.param.R;

/**
 * 系统数据字典表
 * 
 * @author yangyicai
 * @email yangyicai@foxmail.com
 * @date 2021-03-23 13:59:22
 */
 
@Controller
@RequestMapping("/bdzl/dict")
public class DictController extends BaseController {
	@Autowired
	private DictService dictService;
	
	@GetMapping()
	String Dict(){
	    return "bdzl/dict/dict";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<DictDO> dictList = dictService.list(query);
		int total = dictService.count(query);
		PageUtils pageUtils = new PageUtils(dictList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "bdzl/dict/add";
	}

	@GetMapping("/edit/{dictId}")
	String edit(@PathVariable("dictId") Integer dictId,Model model){
		DictDO dict = dictService.get(dictId);
		model.addAttribute("dict", dict);
	    return "bdzl/dict/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( DictDO dict){
		if(dictService.save(dict)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( DictDO dict){
		dictService.update(dict);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Integer dictId){
		if(dictService.remove(dictId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Integer[] dictIds){
		dictService.batchRemove(dictIds);
		return R.ok();
	}
	
}
